<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input  style="width: 200px;"   v-model="query.name" placeholder="姓名" class="filter-item"  ></el-input>
      <el-input  style="width: 200px;"   v-model="query.phone" placeholder="手机" class="filter-item"  ></el-input>


      <el-date-picker class="filter-item"
        v-model="query.registerDay"
        type="datetimerange"
        :picker-options="getPickerOptions"
        range-separator="至"
        start-placeholder="注册时间"
        end-placeholder="注册时间"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd "
      >
      </el-date-picker>
      <el-date-picker class="filter-item"
        v-model="query.givingDay"
        type="datetimerange"
        :picker-options="getPickerOptions"
        range-separator="至"
        start-placeholder="发证时间"
        end-placeholder="发证时间"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd "
      >
      </el-date-picker>
      <br/>
      <el-select v-model="query.useWay" class="filter-item"  placeholder="使用性质">
        <el-option value="all" label="全部" />
        <el-option value="运营" label="运营" />
        <el-option value="非营运" label="非运营" />
        <el-option value="其他" label="其他" />

      </el-select>
      <el-button @click="getRecords" type="primary" class="filter-item">查询</el-button>
      <el-button @click="getRecords" type="danger" class="filter-item">刷新</el-button>
      <el-button @click="exportXls"  class="filter-item">导出</el-button>
    </div>

    <el-table v-loading="listLoading"	:data="list"	element-loading-text="Loading"	border	fit	highlight-current-row>
      <!-- <el-table-column type="selection" width="55" /> -->
      <!-- <el-table-column type="index" label="序列" width="50" /> -->
      <el-table-column prop="id" label="ID" width="50" />
<!--      <el-table-column prop="avatar" label="头像" width="80" >
        <template slot-scope="scope">
          <img v-if="scope.row.avatar" class="table-img" :src="host+scope.row.avatar">
        </template>
      </el-table-column>-->
      <el-table-column prop="belong_to" label="姓名" width="120" />
      <el-table-column prop="car_number" label="车牌" width="120" />
      <el-table-column prop="get_user.phone" label="电话" width="120" />
<!--      <el-table-column prop="get_driver.id_card" label="身份证号" width="180" />-->
<!--      <el-table-column prop="get_driver.gender" label="性别" width="60" />-->
      <el-table-column prop="address" label="地址" min-width="300" />
      <el-table-column prop="car_type" label="类型" width="100" />
      <el-table-column prop="car_brand" label="品牌型号" width="130" />
      <el-table-column prop="use_way" label="使用性质" width="100" />
      <el-table-column prop="spot_code" label="识别代码" width="100" />
      <el-table-column prop="engine_number" label="发动机号" width="100" />
      <el-table-column prop="register_day" label="注册日期" width="100" />
      <el-table-column prop="giving_day" label="发证日期" width="100" />


      <el-table-column prop="get_go_other.file_number" label="档案编号" width="100" />
      <el-table-column prop="get_go_other.all_weight" label="总质量" width="100" />
      <el-table-column prop="get_go_other.carry_number" label="核定载人数" width="100" />
      <el-table-column prop="created_at" label="创建时间" width="160" />
      <!-- <el-table-column prop="status" label="启用/禁用" width="100" >
        <template slot-scope="scope">
            <el-switch	v-model="scope.row.status"	active-color="#409EFF"	inactive-color="#909399"
              :active-value="1"	:inactive-value="2"		@change="changeStatus(scope.row)">
            </el-switch>
        </template>
      </el-table-column> -->
<!--      <el-table-column fixed="right" label="操作" min-width="250">
        <template slot-scope="scope">
          &lt;!&ndash; <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button> &ndash;&gt;
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          <el-button size="mini" type="primary"  @click="resetPassword(scope.$index, scope.row)">重置密码</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.page"
      :page-sizes="[10, 20, 100, 200]"
      :page-size="pagination.pageSize"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total">
    </el-pagination>


  </div>
</template>

<script>
import { myGet } from '@/utils/myRequest'

const urlDriverLst = '/guard/user/goLst';
const urlDriverExportDriver = '/guard/user/goExportXls';
export default {
  data() {
    return {
      host:process.env.VUE_APP_HOST,
      query:{
        name:'',
        phone:'',
        registerDay:'',
        givingDay:'',
        useWay:'all'
      },
      pagination:{
        total:0,
        page:1,
        pageSize:20,
      },
      list: [],
      listLoading: true,
      getPickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      }
    }
  },

  methods:{
    exportXls(){
      this.listLoading = true
      let link = document.createElement("a");
      //创建一个a标签
      link.style.display = "none";
      //将a标签隐藏

      link.href = urlDriverExportDriver+"?"+this.toParams(this.query);
      //给a标签添加下载链接  "域名+接口"  safe是一个动态的域名  后面的接口替换成你自己的下载接口
      link.setAttribute("download", name);
      // 此处注意，要给a标签添加一个download属性，属性值就是文件名称 否则下载出来的文件是没有属性的，空白白
      document.body.appendChild(link);
      //将上面创建的a标签加入到body的尾部
      link.click();
      this.listLoading = false
    },

    toParams(param) {
      var result = ""
      for (let name in param) {
        if (typeof param[name] != 'function') {
          result += "&" + name + "=" + encodeURI(param[name]);
        }
      }
      return result.substring(1)
    },
    reload(){
      Object.assign(this.$data.query,this.$options.data().query)
      Object.assign(this.$data.pagination,this.$options.data().pagination)
      this.getRecords()
    },
    getRecords(){
      this.listLoading = true;
      let params = Object.assign(this.query,this.pagination)
      myGet(urlDriverLst,params).then((res) =>{
        this.listLoading = false;
        if(res.status){
          this.list = res.data.records
          this.pagination.total = res.data.total
        }
      })
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.pagination.page = 1
      this.getRecords()
    },
    handleCurrentChange(val) {
      this.pagination.page = val
      this.getRecords();
    },
  },
  mounted() {
    this.getRecords()
  }
}
</script>

<style scoped>

</style>
